package com.lsh.oms.provider.service.sign;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.dubbo.rpc.protocol.rest.support.ContentType;
import com.lsh.base.common.model.CommonResult;
import com.lsh.oms.api.model.query.QuerySignDto;
import com.lsh.oms.api.model.shipping.ModifyDeliveryExtDto;
import com.lsh.oms.api.service.sign.ISignRestService;
import com.lsh.oms.core.service.sign.core.SignService;
import com.lsh.oms.core.service.tool.Criteria;
import com.lsh.oms.provider.service.BaseService;
import com.lsh.oms.provider.utils.ValidationBeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;

@Path("signs")
@Slf4j
@Service(protocol = "rest", timeout = 30000)
public class SignRestServiceImpl extends BaseService implements ISignRestService {

    @Resource
    private SignService signService;

    @GET
    @Produces({MediaType.APPLICATION_JSON})
    @Override
    public CommonResult query(@Context UriInfo uriInfo,
                              @DefaultValue("1") @QueryParam("page") long page,
                              @DefaultValue("20") @QueryParam("page-limit") long limit) {
        return CommonResult.success(signService.query(Criteria.of(uriInfo), page, limit));
    }

    @GET
    @Path("{id}")
    @Produces({MediaType.APPLICATION_JSON})
    @Override
    public CommonResult findById(@Context UriInfo uriInfo,
                                 @PathParam("id") Long id) {
        return CommonResult.success(signService.findById(id, Criteria.of(uriInfo)));
    }


    /**
     * tms 签收单查询接口
     *
     * @param querySignDto 查询参数
     * @return 返回结果
     */
    @POST
    @Path("/tms/query/signnum")
    @Consumes({MediaType.APPLICATION_JSON})
    @Produces({ContentType.APPLICATION_JSON_UTF_8})
    @Override
    public CommonResult findNumByParam(QuerySignDto querySignDto) {
        log.info("查询签收信息参数 " + querySignDto.toString());

        if (CollectionUtils.isEmpty(querySignDto.getRoute_ids())) {
            log.info("route_ids 参数不能为空");
            return CommonResult.error("route_ids 参数不能为空");
        }

        querySignDto.setIs_valid(1);

        return CommonResult.success(signService.findNumByParam(querySignDto));
    }

    private boolean checkParam(QuerySignDto querySignDto) {

        boolean flag = false;
        if (null == querySignDto) {
            return false;
        }

        if (null == querySignDto.getPn()) {
            querySignDto.setPn(0);
        }

        if (null == querySignDto.getRn()) {
            querySignDto.setRn(100);
        }

        if(null != querySignDto.getAddress_id() && querySignDto.getAddress_id() > 1L){
            querySignDto.setRn(1);
        }

        if (!CollectionUtils.isEmpty(querySignDto.getOrder_ids())) {
            return true;
        }

        if (!CollectionUtils.isEmpty(querySignDto.getRoute_ids())) {
            return true;
        }

        if (!CollectionUtils.isEmpty(querySignDto.getStatuses())) {
            return true;
        }

        if (null != querySignDto.getF_order_id()) {
            return true;
        }
        return flag;
    }

    @POST
    @Path("/proof/modify")
    @Consumes({MediaType.APPLICATION_JSON})
    @Produces({ContentType.APPLICATION_JSON_UTF_8})
    @Override
    public CommonResult modifyExtByDeliveryId(ModifyDeliveryExtDto modifyShippingExtDto) {

        log.info("签收单-修改ext信息参数 " + modifyShippingExtDto.toString());

        if (null == modifyShippingExtDto || CollectionUtils.isEmpty(modifyShippingExtDto.getProof_ids())) {
            log.info("modifyShippingExtDto or proof_ids 参数不能为空");
            return CommonResult.error("modifyShippingExtDto or proof_ids 参数不能为空");
        }

        ValidationBeanUtils.checkRequiredFields(modifyShippingExtDto);

        return CommonResult.success(signService.updateOmsExt(modifyShippingExtDto));
    }
}
